package com.yanqu.xiuxian.gameserver.tripletown.logic;

import com.yanqu.xiuxian.gameserver.tripletown.config.IConfig;
import com.yanqu.xiuxian.gameserver.tripletown.config.IOrderConfig;
import com.yanqu.xiuxian.gameserver.tripletown.data.IUserData;
import com.yanqu.xiuxian.gameserver.tripletown.entity.*;
import com.yanqu.xiuxian.gameserver.tripletown.enums.OrderStatus;
import com.yanqu.xiuxian.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class OrderLogic {

    public static void handleOrder(IConfig config, IUserData userData, int multi, Random random,
                                   EliminateResult eliminateResult, int[][] blockList, Order order) {
        List<Integer> orderIdList = userData.getOrderIdList(multi);
        if (orderIdList == null || orderIdList.isEmpty()) {
            return;
        }
        List<Integer> copyOrderIdList = new ArrayList<>(orderIdList);
        if (order != null && !copyOrderIdList.isEmpty()) {
            EliminateRoundResult eliminateRoundResult = eliminateResult.getLastEliminateRoundResult();
            // 【最后一轮数据的订单  完成/失败  处理】
            OrderStatus orderStatus = getOrderStatus(order);
            switch (orderStatus) {
                case FINISH:
                    // 加炸弹
                    if (BombLogic.addOrderBomb(random, blockList, order, eliminateRoundResult, config)) {
                        // 本轮结果重新设置方块
                        eliminateRoundResult.setBlockList(BlockLogic.copy(blockList));
                    }
                    // 切换下个订单
                    eliminateRoundResult.setFinishOrder(copy(order));
                    checkOrderIdList(copyOrderIdList, multi, config); // 先检查下列表比较稳
                    order = create(copyOrderIdList.remove(0), config);
                    eliminateRoundResult.setCurOrder(copy(order));
                    // 后续订单
                    int finishNextOrderId = getRandomOrderId(multi, config);
                    copyOrderIdList.add(finishNextOrderId);
                    eliminateRoundResult.setAddOrderId(finishNextOrderId);
                    break;
                case FAIL:
                    // 切换下个订单
                    eliminateRoundResult.setFailOrder(copy(order));
                    checkOrderIdList(copyOrderIdList, multi, config); // 先检查下列表比较稳
                    order = create(copyOrderIdList.remove(0), config);
                    eliminateRoundResult.setCurOrder(copy(order));
                    // 后续订单
                    int failNextOrderId = getRandomOrderId(multi, config);
                    copyOrderIdList.add(failNextOrderId);
                    eliminateRoundResult.setAddOrderId(failNextOrderId);
                    break;
            }
            eliminateResult.setCurOrder(copy(order));
            eliminateResult.setOrderIdList(new ArrayList<>(copyOrderIdList));
        }
    }

    /**
     * 创建
     */
    public static Order create(int orderId, IConfig config) {
        // 获取配置
        IOrderConfig orderConfig = config.getOrderConfigMap().get(orderId);

        Order order = new Order();
        order.setOrderId(orderConfig.getOrderId());
        order.setCurStep(0);
        order.setMaxStep(orderConfig.getMaxTimes());

        Map<Integer, OrderProg> progMap = new ConcurrentHashMap<>();
        List<Integer> numList = orderConfig.getNumList();
        List<Integer> blockList = orderConfig.getBlockList();
        for (int i = 0; i < numList.size(); i++) {
            progMap.put(blockList.get(i), new OrderProg(blockList.get(i), numList.get(i), 0));
        }
        order.setProgMap(progMap);

        order.setReward(orderConfig.getReward());

        return order;
    }

    /**
     * 复制
     */
    public static Order copy(Order order) {
        return order == null ? null : order.clone();
    }

    /**
     * 获取订单状态
     */
    public static OrderStatus getOrderStatus(Order order) {
        boolean finish = true;
        for (OrderProg orderProg : order.getProgMap().values()) {
            if (orderProg.getCurNum() < orderProg.getNeedNum()) {
                finish = false;
                break;
            }
        }
        if (order.getCurStep() >= order.getMaxStep()) {
            return finish ? OrderStatus.FINISH : OrderStatus.FAIL;
        } else {
            return finish ? OrderStatus.FINISH : OrderStatus.PROGRESS;
        }
    }

    /**
     * 获取随机订单ID
     */
    public static int getRandomOrderId(int multi, IConfig config) {
        List<Integer> idList = new ArrayList<>();
        List<Integer> weightList = new ArrayList<>();
        for (IOrderConfig orderConfig : config.getOrderConfigMap().values()) {
            if (orderConfig.getMulti() == 0 && multi == 1
                    || orderConfig.getMulti() == 1 && multi == 8
                    || orderConfig.getMulti() == 2 && multi == 16) {
                idList.add(orderConfig.getOrderId());
                weightList.add(orderConfig.getWeight());
            }
        }
        int index = RandomHelper.getRandomIndexByWeight(weightList);
        IOrderConfig orderConfig = config.getOrderConfigMap().get(idList.get(index));
        return orderConfig.getOrderId();
    }

    /**
     * 补充（修复）后续订单数据
     */
    public static void checkOrderIdList(List<Integer> orderIdList, int multi, IConfig config) {
        // 有问题的删了
        Map<Integer, ? extends IOrderConfig> configMap = config.getOrderConfigMap();
        for (int i = orderIdList.size() - 1; i >= 0; i--) {
            if (i > 2 || !configMap.containsKey(orderIdList.get(i))) {
                orderIdList.remove(i);
            }
        }
        // 少的加上去
        for (int i = orderIdList.size(); i <= 2; i++) {
            orderIdList.add(getRandomOrderId(multi, config));
        }
    }

    /**
     * 添加任务进度
     */
    public static void addOrderProg(Order copyOrder, Map<Integer, Map<Integer, Block>> eliminateMap) {
        for (Map<Integer, Block> blockMap : eliminateMap.values()) {
            for (Block block : blockMap.values()) {
                OrderProg orderProg = copyOrder.getProgMap().get(block.getC());
                if (orderProg != null) {
                    orderProg.incrementCurNum();
                }
            }
        }
    }

}
